package com.android.whereismy.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class CmnUtils {
    
    /**
     * isStrEmpty.
     * @param str
     * @return true when parameter is NULL or empty
     */
    public static boolean isStrEmpty(String str) {
        if (str == null || str.length() == 0) {
            return true;
        } else {
            return false;
        }
    }

    public static Map<String, Boolean> getComInfo(ConnectivityManager cm) {

        boolean isMobileAvail = false;
        boolean isMoblieCon = false;
        boolean isWifiAvail = false;
        boolean isWifiCon = false;
        
        try {
            NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (ni.isAvailable()) {
                isMobileAvail = true;
            }
            if (ni.isConnected()) {
                isMoblieCon = true;
            }
            
            ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (ni.isAvailable()) {
                isWifiAvail = true;
            }
            if (ni.isConnected()) {
                isWifiCon = true;
            }
            
        } catch (Exception e) {
            Log.d(CmnConsts.DEBUG_TAG, e.toString());
        }
        
        Map<String, Boolean> resultMap = new HashMap<String, Boolean>();
        resultMap.put(CmnConsts.MOBILE_STAT, isMobileAvail);
        resultMap.put(CmnConsts.MOBILE_CON, isMoblieCon);
        resultMap.put(CmnConsts.WIFI_STAT, isWifiAvail);
        resultMap.put(CmnConsts.WIFI_CON, isWifiCon);
        
        return resultMap;
    }
    
    public static Map<String, Boolean> getLocInfo(LocationManager locationMgr) {

        boolean isNetworkLocationActivated = false;
        boolean isGpsLocationActivated = false;
        
        try {
            
            if (locationMgr.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
                isNetworkLocationActivated = true;
            }
            if (locationMgr.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                isGpsLocationActivated = true;
            }
        } catch (Exception e) {
            Log.d(CmnConsts.DEBUG_TAG, e.toString());
        }
        
        Map<String, Boolean> resultMap = new HashMap<String, Boolean>();
        resultMap.put(CmnConsts.GPS_STAT, isGpsLocationActivated);
        resultMap.put(CmnConsts.NETWORK_STAT, isNetworkLocationActivated);
        
        return resultMap;
    }
    
    /** 
     * getMD5 for input String
     * 
     * @param input
     * @return MD5snippet
     */
    public static String getMd5Hash(String input) {
        try     {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            BigInteger number = new BigInteger(1,messageDigest);
            String md5 = number.toString(16);
            
            while (md5.length() < 32)
                md5 = "0" + md5;
                return md5;
        } catch(NoSuchAlgorithmException e) {
                Log.e(CmnConsts.HEADER, e.getMessage());
            return null;
        }
    }
    
    /**
     * getFreshLevel.
     * <BR>
     * return fresh level that calculated by time-gap 
     * between reported time and current time<BR>
     * @param rptTime reported time
     * @return freshLevel
     */
    public static String getFreshLevel(final Date rptTime) {
        // get time-gap between reported time and current time
        long timeGap = System.currentTimeMillis() - rptTime.getTime();

        // check fresh_level
        if (timeGap < CmnConsts.FRESH_LEVEL_TIME_HIGH) {
            return CmnConsts.FRESH_LEVEL_HIGH;
        } else if (timeGap < CmnConsts.FRESH_LEVEL_TIME_MID) {
            return CmnConsts.FRESH_LEVEL_MID;
        } else {
            return CmnConsts.FRESH_LEVEL_LOW;
        }
    }
}
